package com.dnuifoodqgx618.dnuifood_16110100618.controller;

import android.content.Intent;
import android.net.Uri;
import android.speech.RecognitionListener;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.dnuifoodqgx618.dnuifood_16110100618.R;
import com.dnuifoodqgx618.dnuifood_16110100618.bean.ResultBean;
import com.dnuifoodqgx618.dnuifood_16110100618.bean.UserBean;
import com.dnuifoodqgx618.dnuifood_16110100618.listener.OnRetrofitListener;
import com.dnuifoodqgx618.dnuifood_16110100618.model.UserModel;

public class RegisterActivity extends BaseActivity implements OnRetrofitListener<ResultBean> {
    private Intent intent;
    private ImageView reg_return;
    private Button register;
    private EditText et_reg_user,et_reg_password,et_reg_repassword,
            et_reg_telephone,et_reg_adress,et_reg_comment;
    private UserModel userModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
        initEvents();
      /*  Bundle bundle = new Bundle();       //携带成功注册的用户名参数
        bundle.putString("data","test");
        Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);*/
    }
    @Override
    void initViews() {
        setLayout(R.layout.activity_register);
        register = findViewById(R.id.register);
        reg_return = findViewById(R.id.reg_return);
        et_reg_adress = findViewById(R.id.et_reg_adress);
        et_reg_comment = findViewById(R.id.et_reg_comment);
        et_reg_password = findViewById(R.id.et_reg_password);
        et_reg_telephone = findViewById(R.id.et_reg_telephone);
        et_reg_repassword = findViewById(R.id.et_reg_repassword);
        et_reg_user = findViewById(R.id.et_erg_user);
    }

    @Override
    void initEvents() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_reg_password.getText().toString().equals(et_reg_repassword.getText().toString()))
                {
                    register();
                }
                else{
                    showToast("请确认两次密码相同");
                }
            }
        });


        reg_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                intent = new Intent();
                intent.putExtra("user",et_reg_user.getText().toString());
                setResult(RESULT_OK,intent);
            }
        });
    }

    @Override
    void initData() {

    }
    @Override
    public void onsuccess(ResultBean resultBean, int flag) {
        if(resultBean.getSuccess().equals("1")){
            intent = new Intent();
            intent.putExtra("user",et_reg_user.getText().toString());
            setResult(RESULT_OK,intent);
            finish();
            showToast("注册成功");
        }
        else {
            showToast("注册失败");
        }
    }

    @Override
    public void onFailure(String string) {
        showToast("网络发生错误");
    }

    private void register(){
        userModel = new UserModel();
        userModel.register(et_reg_user.getText().toString(),et_reg_password.getText().toString(),et_reg_telephone.getText().toString(),et_reg_adress.getText().toString(),et_reg_comment.getText().toString(),this);
    }
}

